package com.wetask.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.wetask.dao.BannerDao;
import com.wetask.domain.model.Banner;
import com.wetask.domain.model.PagingInfo;
import com.wetask.domain.model.SearchResult;
import com.wetask.model.vo.BannerVO;
import com.wetask.utils.DaoHelper;

@Repository
public class BannerDaoImpl extends DaoHelper<Banner> implements BannerDao{
	
	@SuppressWarnings("rawtypes")
	class BannerRowMaper implements RowMapper {
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			Banner banner = new Banner();
			banner.setId(rs.getInt("id"));
			banner.setActive(rs.getBoolean("active"));
			banner.setNameFile(rs.getString("name_file"));
			banner.setType(rs.getString("type"));
			banner.setUrlFile(rs.getString("url_file"));
			banner.setCreateDate(rs.getTimestamp("create_date"));
			return banner;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public SearchResult<Banner> getBanner(PagingInfo pagingInfo, String keyword) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM banner WHERE active = 1 ");
		if (StringUtils.isNotEmpty(keyword)) {
			sql.append("AND (");
			String[] keyStrings = keyword.split(" ");
			for (int i = 0; i < keyStrings.length; i++) {
				String s = "'%" + keyStrings[i] + "%'";
				if (i == 0) {
					sql.append(" name_file LIKE " + s );
				} else {
					sql.append("OR name_file LIKE " + s );
				}
			}
			sql.append(")");
		}
		sql.append(" ORDER BY create_date DESC");
		return queryPaging(sql.toString(), null, new BannerRowMaper(),
				pagingInfo);
	}

	@Override
	public void saveBanner(BannerVO bannerVO) {
		String sql= "Insert into banner (id,name_file,url_file,type,create_date,active) Value (?,?,?,?,?,?)";
		Object[] params = new Object[] {bannerVO.getId(),bannerVO.getNameFile(),bannerVO.getUrlFile(),bannerVO.getType(),bannerVO.getCreateDate(),bannerVO.isActive()};
		insert(sql, params);
	}

	@Override
	public void deleteBanner(int id) {
		String sql= "Update banner set active = 0 Where id = ?";
		Object[] params = new Object[] {id};
		insert(sql, params);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public Banner getBannerDetail(int id) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM banner B WHERE B.id = ?");
		Object[] param = new Object[] { id };
		return query(sql.toString(), param, new BannerRowMaper())
				.getSearchResult().get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public SearchResult<Banner> getBannerByType(PagingInfo pagingInfo, String type) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM banner WHERE active = 1 AND type=? ");
		sql.append(" ORDER BY create_date DESC");
		Object[] param = new Object[]{type};
		
		return queryPaging(sql.toString(), param, new BannerRowMaper(), pagingInfo);
	}
}
